// components/plan/plan.js
const util = require("../../utils/util");
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    cardOnsale: {
      type: Object,
      value: null
    },
    share:{
      type:String,
      value: ''
    },
    ischannel:{
      type:String,
      value: ''
    },
    renew:{
      type:Boolean, 
      value:''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    imgPath: "http://qnstatic.lianhequnmei.com/images",
    agreeValue: 0,
    animationData_notice: 0,
    members: [
      '李**在15分钟前成功加入宝宝成长计划',
      'han**在5分钟前成功加入宝宝成长计划',
      '张**在8分钟前成功加入宝宝成长计划',
      '玲**在17分钟前成功加入宝宝成长计划',
      '陈**在20分钟前成功加入宝宝成长计划',
      '王**在5分钟前成功加入宝宝成长计划',
      '单**在10分钟前成功加入宝宝成长计划',
      'Lil**在9分钟前成功加入宝宝成长计划',
      '单**在21分钟前成功加入宝宝成长计划',
      'xi**在18分钟前成功加入宝宝成长计划',
      '马**在22分钟前成功加入宝宝成长计划',
      'han**在5分钟前成功加入宝宝成长计划',
      '李**在18分钟前成功加入宝宝成长计划',
      '王**在12分钟前成功加入宝宝成长计划',
      '李**在20分钟前成功加入宝宝成长计划',
      '文**在5分钟前成功加入宝宝成长计划',
      '李**在15分钟前成功加入宝宝成长计划',
      'han**在5分钟前成功加入宝宝成长计划',
      '张**在8分钟前成功加入宝宝成长计划',
      '玲**在17分钟前成功加入宝宝成长计划',
      '陈**在20分钟前成功加入宝宝成长计划',
      '王**在5分钟前成功加入宝宝成长计划',
      '黄**在7分钟前成功加入宝宝成长计划',
      'han**在5分钟前成功加入宝宝成长计划',
      '张**在8分钟前成功加入宝宝成长计划',
      '林**在11分钟前成功加入宝宝成长计划',
      '陈**在20分钟前成功加入宝宝成长计划',
      '王**在5分钟前成功加入宝宝成长计划',
      '文**在15分钟前成功加入宝宝成长计划',
      '李**在5分钟前成功加入宝宝成长计划',
      '张**在8分钟前成功加入宝宝成长计划',
      '黄**在17分钟前成功加入宝宝成长计划',
      '王**在20分钟前成功加入宝宝成长计划',
      '玲**在5分钟前成功加入宝宝成长计划',
    ],
    marqueePace: 1, //滚动速度
    marqueeDistance: 0, //初始滚动距离
    size: 28,
    marqueeWidth: 660,
    marqueeMargin: 40,
    isIPhoneX: false,
    checked: true,
    share:'',
    ischannel:''

  },

  /**
   * 组件的方法列表
   */
  methods: {
    
 shuffle(arr) {
      let length = arr.length,
        randomIndex,
        temp;
      while (length) {
        randomIndex = Math.floor(Math.random() * (length--));
        temp = arr[randomIndex];
        arr[randomIndex] = arr[length];
        arr[length] = temp
      }
      return arr;
    },
    gozhuce(event){
     let type = event.target.dataset.type;
    // let url  = 'pages/agreement/agreement/?type=2' //+ type;
     let url = '/pages/agreement/agreement?&type='+type;///?type='+type
     wx.navigateTo({
        url: url,
      });
    },
    onChange(event) {
      this.setData({
        checked: event.detail,
      });
    },
    getequipment() {
      let screenHeight = wx.getSystemInfoSync().screenHeight
      let bottom = wx.getSystemInfoSync().safeArea.bottom
      let flag = screenHeight !== bottom
      this.setData({
        isIPhoneX: flag
      })
    },
    kaiTong() {
      wx.showToast({
        title: '用成长卡开通',
      })
    },
    userCard() {
      wx.navigateTo({
        url: '/pages/chengZhangKa/index',
      })
    },
    serviceDetails(){
    
    },
    goplan(e){
      let ids = e.currentTarget.dataset.id
      wx.navigateTo({
        url: '/pages/serviceDetails/serviceDetails?id='+ ids,
      })
    },
    /**
     * 立即开通
     */
    liZhiKaiTong() {

      const api = require("../../api/api")
      api.order.transactions({
        goods_info: [{
          goods_id: this.data.cardOnsale.id,
          buy_number: 1,
          coupon_id: "",
        }],
        channel: wx.getStorageSync('channel'),
        is_share: this.data.share,
        price: this.data.cardOnsale.price,
        success(res) {
          // console.log("transactions.res", res)
          let order_sn = res.data.data.order_sn;
          let is_share = wx.getStorageSync('is_share');
          api.order.payment({
            order_sn: order_sn,
            pay_type: 'wx',
            pay_client: 'lite',
            is_share,
            success(resPayment) {
              // console.log("payment,resPayment", resPayment)

              let dataPayment = resPayment.data.data;

              //微信支付
              wx.requestPayment({
                timeStamp: dataPayment.timeStamp,
                nonceStr: dataPayment.nonceStr,
                package: dataPayment.package,
                signType: 'MD5',
                paySign: dataPayment.paySign,
                success(resPay) {
                  // console.log("wx.requestPayment,success,resPay", resPay)

                  //支付成功
                  // wx.navigateTo({
                  //   url: '/pages/orderSheet/paySuccess',
                  // })
                  wx.navigateTo({
                    url: '/pages/index/index?query=paySuccess',
                  })
                  
                },
                fail(resPay) {
                  // console.log("wx.requestPayment,fail,resPay", resPay);
                  //支付失败

                  //调订单删除接口
                  api.user.orderDelete({
                    order_sn: order_sn,
                    success(resDel) {
                      // console.log("orderDelete,resDel",resDel)
                    }
                  })

                  wx.navigateTo({
                    url: '/pages/orderSheet/payFail?jp=/pages/plan/joinPlan',
                  })
                }
              })
            }
          })
        }
      })

      // wx.navigateTo({
      //   url: '/pages/orderSheet/paying',
      // })
    },

  },
  lifetimes: {
    created(){

     },
     ready(){
          let that = this;
          this.setData({
          //  imgPath: util.getImgPath(),
            share:this.data.share,
            ischannel:this.data.ischannel
          })
           //当前在售成长卡
           const api = require("../../api/api");
          //  api.user.home({
          //    success(resHome) {
          //      // console.log("home,resHome", resHome)
          //    }
          //  });
           const is_share = this.data.share; //wx.getStorageSync('is_Share');  // 生命周期问题获取不到
           const channel =  this.data.ischannel;//wx.getStorageSync('channel');
           api.card.cardOnsale({
              is_share: is_share?is_share:'',
              channel: channel?channel:'',
             success(res) {
               //  console.log("cardOnsale,res", res)
              // console.log(res.data.data.card_buyers);
     
               if(res.data.code == 0){
                  let datas = res.data.data.card_buyers;
                
                  if(Object.keys(datas).length>0){
                    that.setData({
                      members: that.data.members.concat(res.data.data.card_buyers) 
                    }) 
                  } 
                  that.setData({
                    cardOnsale: res.data.data,
                  })
               }

         
             }
           })
     },
    attached() {
      let self = this;
      const util = require("../../utils/util")
      this.getequipment();
      wx.setNavigationBarTitle({
        title: '加入计划',
      })
      let width = wx.getSystemInfoSync().screenWidth*7;
      function carousel_num() {
        let animation = wx.createAnimation({
          transformOrigin: "50% 50%",
          duration: 65000,
          timingFunction: "linear",
          delay: 0
        })
        self.animation = animation
        animation.translate3d(-width, 0, 100).step()
        //console.log("第一动画开始");
        self.setData({
          animationData_notice: animation.export(),
        })
        setTimeout(function () {
          let animation = wx.createAnimation({
            transformOrigin: "50% 50%",
            duration: 0,
            timingFunction: "linear",
            delay: 0
          })
          self.animation = animation
          animation.translate3d(0, 0, 100).step()
          //console.log("第二动画开始");
          self.setData({
            animationData_notice: animation.export(),
          })
          setTimeout(function () {
            carousel_num();
          }, 50)
          let arr = self.shuffle(self.data.members);
          self.setData({
            members: arr
          });
        }, 50000)  //间隔多久第二次
      }
      setTimeout(function () {
        carousel_num();
      }, 1000);

    }
  },
  observers: {
    // channel: function (val) {
    //   this.setData({
    //     is_channel: val,
    //   })
    // },
    // is_share: function (val) {
    //   console.log('is_share65',val)
    //   this.setData({
    //     share: val,
    //   })
    // },
  },
})